package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.ServerActivityMallDayBuyData;

import java.sql.*;
import java.util.*;

public class ServerActivityMallDayBuyDaoImpl extends TempDataObjectDao<ServerActivityMallDayBuyData> {


    public static String getServerActivityMallDayBuyDataSql = "select * from `t_u_activity_mall_day_buy` where `activityId`=?";
    public List<ServerActivityMallDayBuyData> getServerActivityMallDayBuyDataList(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<ServerActivityMallDayBuyData> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getServerActivityMallDayBuyDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ServerActivityMallDayBuyData data = parse(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String updateServerActivityMallDayBuyDataSql = "update `t_u_activity_mall_day_buy` set buyCount=?,awardConditionId=? where activityId=? and mallId=? and day=? and userId=?";
    @Override
    protected boolean updateData(ServerActivityMallDayBuyData serverActivityMallDayBuyData) {
        serverActivityMallDayBuyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, serverActivityMallDayBuyData.getBuyCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(serverActivityMallDayBuyData.getAwardConditionIdList())));
        para.put(i++, new DbParameter(Types.INTEGER, serverActivityMallDayBuyData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, serverActivityMallDayBuyData.getMallId()));
        para.put(i++, new DbParameter(Types.INTEGER, serverActivityMallDayBuyData.getDay()));
        para.put(i++, new DbParameter(Types.BIGINT, serverActivityMallDayBuyData.getUserId()));
        if (getDb().executeNoneQuery(updateServerActivityMallDayBuyDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertServerActivityMallDayBuyDataSql = "insert into `t_u_activity_mall_day_buy`(activityId,mallId,day,userId,buyCount,awardConditionId) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(ServerActivityMallDayBuyData serverActivityMallDayBuyData) {
        serverActivityMallDayBuyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, serverActivityMallDayBuyData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, serverActivityMallDayBuyData.getMallId()));
        para.put(i++, new DbParameter(Types.INTEGER, serverActivityMallDayBuyData.getDay()));
        para.put(i++, new DbParameter(Types.BIGINT, serverActivityMallDayBuyData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, serverActivityMallDayBuyData.getBuyCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(serverActivityMallDayBuyData.getAwardConditionIdList())));
        if (getDb().executeNoneQuery(insertServerActivityMallDayBuyDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ServerActivityMallDayBuyData parseData(ResultSet rs) throws SQLException {
        ServerActivityMallDayBuyData serverActivityMallDayBuyData = new ServerActivityMallDayBuyData();
        serverActivityMallDayBuyData.setActivityId(rs.getInt("activityId"));
        serverActivityMallDayBuyData.setMallId(rs.getInt("mallId"));
        serverActivityMallDayBuyData.setDay(rs.getInt("day"));
        serverActivityMallDayBuyData.setUserId(rs.getLong("userId"));
        serverActivityMallDayBuyData.setBuyCount(rs.getInt("buyCount"));
        serverActivityMallDayBuyData.setAwardConditionIdList(JSON.parseObject(rs.getString("awardConditionId"), new TypeReference<HashSet<Integer>>() {}));

        return serverActivityMallDayBuyData;
    }
}
